﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.UI.Page;
using E.Web.Common;

using X.Common;

namespace E.Web
{
    public partial class ForgetPwd : BasePage
    {
        public string act = "";
        public bool err = false;
        public string question = "";
        public int tmpuserid;

        protected override void ShowPage()
        {
            act = XRequest.GetString("act");

            if (act.Equals(""))
                err = true;

            if (ispost)
            {
                UserBLL bll = new UserBLL();
                switch (act)
                {
                    case "l":
                        string loginid = XRequest.GetString("LoginIDTxt");
                        if (!loginid.Equals(""))
                        {
                            IDictionary<int, string> info = bll.GetForgetPwdInfoByLoginID(loginid);
                            if (info.Count > 0)
                            {
                                foreach (KeyValuePair<int, string> kvp in info)
                                {
                                    tmpuserid = kvp.Key;
                                    question = kvp.Value;
                                }
                            }
                            else
                                err = true;
                        }
                        else
                            err = true;
                        break;
                    case "q":
                        tmpuserid = XRequest.GetInt("Tmpid", -1);
                        string answer = XRequest.GetString("AnswerTxt");
                        if (tmpuserid != -1 && !answer.Equals(""))
                        {
                            tmpuserid = bll.CheckForgetPwdAnswer(tmpuserid, answer);
                            if (tmpuserid != -1)
                            { }
                            else
                                err = true;
                        }
                        else
                            err = true;
                        break;
                    case "u":
                        tmpuserid = XRequest.GetInt("Tmpid2", -1);
                        string pwd = XRequest.GetString("NewPwdTxt");
                        string confirmPwd = XRequest.GetString("ConfirmNewPwdTxt");
                        if (!pwd.Equals("") && !confirmPwd.Equals("") && pwd == confirmPwd && pwd.Length >= 6 && pwd.Length <= 12 && tmpuserid != -1)
                        {
                            int rst = bll.UpdatePassword(tmpuserid, Utils.MD5(pwd));
                            if (rst <= 0)
                            {
                                err = true;
                            }
                        }
                        else
                            err = true;
                        break;
                    default:
                        err = true;
                        break;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
